package com.mycompany.j2eem.service.wucyservice;

import com.mycompany.j2eem.Entity.*;
import com.mycompany.j2eem.Mapper.wcymapper.PersonalHubMapper;
import com.mycompany.j2eem.service.common.Ftp;
import com.mycompany.j2eem.Entity.MyThread;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

/**
 *
 * @author asus
 */
@Service
public class PersonHubService {

    @Autowired
    PersonalHubMapper personalHubMapper;

    //改变图片权限
    public JsonResult changeState(String pictureid) {
        String state = personalHubMapper.getPrivate(pictureid);
        if (state.equals("public")) {
            personalHubMapper.change(pictureid, "private");
        } else {
            personalHubMapper.change(pictureid, "public");
        }
        return new JsonResult(ResultCode.SUCCESS);
    }

    public JsonResult changeState(List<String> pictureid) {
        pictureid.stream().forEach(x -> {
            String state = personalHubMapper.getPrivate(x);
            if (state.equals("public")) {
                personalHubMapper.change(x, "private");
            } else {
                personalHubMapper.change(x, "public");
            }
        });
        return new JsonResult(ResultCode.SUCCESS);
    }

    //获取收藏夹列表
    public JsonResult getFavoriteList(String username) {
        String Account_id = personalHubMapper.getAccountId(username);
        List<FavoriteId> list = personalHubMapper.getFavoriteName(Account_id);
        return new JsonResult(ResultCode.SUCCESS, list);
    }

    //收藏图片
    public JsonResult favoritePicture(String id, String favoriteid) {
        personalHubMapper.favoritePicture(favoriteid, id);
        return new JsonResult(ResultCode.SUCCESS);
    }

    //创建收藏夹
    public JsonResult createFavorite(String favoriteName, String username) {
        String Account_id = personalHubMapper.getAccountId(username);
        InsertName insertName = new InsertName();
        insertName.setName(favoriteName);
        personalHubMapper.createFavorite(insertName);
        String id = insertName.getId();
        personalHubMapper.create_favorite_two(Account_id, id);
        return new JsonResult(ResultCode.SUCCESS, "创建成功");
    }

    //移动到其他收藏夹
    public JsonResult moveFavorite(String newFavoriteId, String pictureId, String username) {
        String AccountId = personalHubMapper.getAccountId(username);
        personalHubMapper.changeFavorite(newFavoriteId, pictureId, AccountId);
        return new JsonResult(ResultCode.SUCCESS, "移动成功");
    }

    //删除收藏夹分类
    public JsonResult deleteFavorite(String favoriteId) {
        personalHubMapper.delteFavorite(favoriteId);
        personalHubMapper.deleteMyfav(favoriteId);
        return new JsonResult(ResultCode.SUCCESS, "删除成功");
    }

    //取消收藏
    public JsonResult cancelFavorite(String pictureId, String favoriteId) {
        personalHubMapper.cancelFavorite(pictureId, favoriteId);
        return new JsonResult(ResultCode.SUCCESS, "取消成功");
    }

    //个人仓库创建分类
    public JsonResult createPersonalHub(String PersonalHubName, String username) {
        String Account_id = personalHubMapper.getAccountId(username);
        InsertName insertName = new InsertName();
        insertName.setName(PersonalHubName);
        personalHubMapper.createPersonalHub(insertName);
        String id = insertName.getId();
        personalHubMapper.createPersonalHubtwo(Account_id, id);
        return new JsonResult(ResultCode.SUCCESS, "创建成功");
    }

    //移动到个人仓库其他分类
    public JsonResult movePersonlHub(String newPersonalHubId, String pictureId, String username) {
        String AccountId = personalHubMapper.getAccountId(username);
        personalHubMapper.changePersonalHub(newPersonalHubId, pictureId, AccountId);
        return new JsonResult(ResultCode.SUCCESS);
    }

    //删除个人仓库分类
    public JsonResult deletePersonalHub(String HubId) {
        personalHubMapper.deletePersonalHub(HubId);
        personalHubMapper.deleteMyPersonalHub(HubId);
        return new JsonResult(ResultCode.SUCCESS);
    }

    //删除个人仓库图片
    public JsonResult deletePersonalPicture(String pictureId, String PersonalHubId) {
        personalHubMapper.cancelPersonalHub(pictureId, PersonalHubId);
        return new JsonResult(ResultCode.SUCCESS, "删除成功");
    }

    //删除个人仓库图片(批量删除)
    public JsonResult deletePersonalPicture(List<String> pictureId, String PersonalHubId) {
        pictureId.stream().forEach(x -> {
            personalHubMapper.cancelPersonalHub(x, PersonalHubId);
        });
        return new JsonResult(ResultCode.SUCCESS, "删除成功");
    }

    //获取个人仓库列表
    public JsonResult getPersonalHubList(String username) {
        String Account_id = personalHubMapper.getAccountId(username);
        List<PersonalHubId> list = personalHubMapper.getPersonalHubName(Account_id);
        return new JsonResult(ResultCode.SUCCESS, list);
    }

    //上传到个人仓库
    public JsonResult upLoad(MultipartFile file, String PersonalHubId) {
        List<String> listName = new ArrayList<>();
        List<MultipartFile> muFile = new ArrayList<>();
        muFile.add(file);
        Ftp ftp = new Ftp();
        try {
            listName = ftp.testFtpClient(muFile);
        } catch (Exception ex) {
            Logger.getLogger(PersonHubService.class.getName()).log(Level.SEVERE, null, ex);
        }
        listName.stream().forEach(x -> {
            String path = "/images/" + x;
            InsertName insertName = new InsertName();
            insertName.setName(path);
            personalHubMapper.upLoadPicture(insertName);
            String id = insertName.getId();
            personalHubMapper.personalHubPicture(PersonalHubId, id);
        });

        return new JsonResult(ResultCode.SUCCESS);
    }

    //发布
    public JsonResult fabu(List<String> imagesId, List<String> tagName, String username, String title, String available) {
        String Account_id = personalHubMapper.getAccountId(username);
        Movement movement = new Movement();
        movement.setAccountId(Account_id);
        movement.setTitle(title);
        movement.setAvailable(available);
        personalHubMapper.createMovement(movement);
        String moId = movement.getId();
        List<String> tagIdList = new ArrayList<>();
        tagName.stream().forEach(x -> {
            String id = personalHubMapper.getTagId(x);
            tagIdList.add(id);
        });
        tagIdList.stream().forEach(x -> {
            personalHubMapper.moveTag(moId, x);
        });
        imagesId.stream().forEach(x -> {
            tagIdList.stream().forEach(y -> {
                personalHubMapper.addAttritube(x, y);
            });
            personalHubMapper.pictureMovement(moId, x);
            personalHubMapper.updateState(x);
        });

        return new JsonResult(ResultCode.SUCCESS);
    }

    //新建标签关系
    public JsonResult addTag(List<String> tagName) {
        List<String> tagId = new ArrayList<>();
        tagName.stream().forEach(x -> {
            if (personalHubMapper.ifTagExist(x) == null) {
                InsertName insertName = new InsertName();
                insertName.setName(x);
                insertName.setCount(0);
                personalHubMapper.addTagName(insertName);
                String id = insertName.getId();
//                personalHubMapper.addTag(id);
                tagId.add(id);
            } else {
                tagId.add(personalHubMapper.ifTagExist(x));
            }
        });
        for (int i = 0; i < tagId.size(); i++) {
            if (i != tagName.size() - 1) {
                personalHubMapper.addTagChildrenReationship(tagId.get(i), tagId.get(i + 1), "0");
            }
            if (i != 0) {
                personalHubMapper.addTagParentReationship(tagId.get(i), tagId.get(i - 1), "0");
            }
        }
        return new JsonResult(ResultCode.SUCCESS);
    }

    //获取仓库图片
    public JsonResult getHubPicture(String hubID, String username) {
        String account = personalHubMapper.getAccountId(username);
        List<String> list = personalHubMapper.getMyHubId(account);
        if (list.contains(hubID)) {
            return new JsonResult(ResultCode.SUCCESS, personalHubMapper.getHubPath(hubID));
        } else {
            return new JsonResult(ResultCode.FAIL, "此仓库已关闭", "");
        }
    }

    //获取收藏夹图片
    public JsonResult getFavoritePicture(String hubID) {

        return new JsonResult(ResultCode.SUCCESS, personalHubMapper.getFavoritePath(hubID));
    }

    //喜欢
    public JsonResult likePicture(String username, String pictureId) {
        String accountId = personalHubMapper.getAccountId(username);
        String temp = personalHubMapper.getIsLike(accountId, pictureId);
        if(temp != null){
            personalHubMapper.deletePicture(accountId, pictureId);
            return new JsonResult(ResultCode.SUCCESS);
        }
        personalHubMapper.likePicture(accountId, pictureId);
        return new JsonResult(ResultCode.SUCCESS);
    }
    
    //获取喜欢列表
    public JsonResult likeList(String username){
        String accountId = personalHubMapper.getAccountId(username);
        List<String> temp = personalHubMapper.getLikePicture(accountId);
        
        return new JsonResult(ResultCode.SUCCESS,temp);
    }

    //获取动态信息
    public JsonResult getMovementInformation(String username) {
        String accountId = personalHubMapper.getAccountId(username);
        List<NewMovement> temp = personalHubMapper.getMovement(accountId);
        List<ShowMovement> showMovement = new ArrayList<>();
        temp.stream().forEach(x -> {
//            if (x.getAvailable().equals("1")) {
            List<PictureId> picturePath = personalHubMapper.getMovementPicture(x.getId());
            ShowMovement showMovement1 = new ShowMovement(x.getId(), x.getTitle(), x.getTime(), picturePath);
            showMovement1.isavailable(x.getAvailable());
            showMovement.add(showMovement1);
//            }
        });
        return new JsonResult(ResultCode.SUCCESS, showMovement);
    }

    //看别人动态
    public JsonResult getOtherMovementInformation(String username) {
//        String accountId = personalHubMapper.getAccountId(username);
        List<NewMovement> temp = personalHubMapper.getMovement(username);
        List<ShowMovement> showMovement = new ArrayList<>();
        temp.stream().forEach(x -> {
            if (x.getAvailable().equals("1")) {
                List<PictureId> picturePath = personalHubMapper.getMovementPicture(x.getId());
                ShowMovement showMovement1 = new ShowMovement(x.getId(), x.getTitle(), x.getTime(), picturePath);
                showMovement.add(showMovement1);
            }
        });
        return new JsonResult(ResultCode.SUCCESS, showMovement);
    }

    public JsonResult getFollowMovement(String username) {
        String accountId = personalHubMapper.getAccountId(username);
        List<String> followList = personalHubMapper.getFollowList(accountId);
        List<PictureId> pictureList = new ArrayList<>();
        followList.stream().forEach(x -> {
            List<String> MovementId = personalHubMapper.getMovementId(x);
            MovementId.stream().forEach(y->{
                List<PictureId> temp = personalHubMapper.getMovementPicture(y);
                pictureList.addAll(temp);
            });           
        });
        pictureList.stream().forEach(x->{
            x.initId();
        });
        return new JsonResult(ResultCode.SUCCESS, pictureList);

    }

    //多线程查找
    public JsonResult getSelect(String username, String tagNameId) {

        MyThread R1 = new MyThread("1");
        R1.start();

        MyThread R2 = new MyThread("2");
        R2.start();

        MyThread R3 = new MyThread("3");
        R3.start();
        return new JsonResult(ResultCode.SUCCESS);
    }

    //判断是否收藏
    public JsonResult isFavorite(String username, String pictureId) {
        String accountId = personalHubMapper.getAccountId(username);
        List<String> temp = personalHubMapper.getFavoritePicture(accountId);
        if (temp.contains(pictureId)) {
            return new JsonResult(ResultCode.SUCCESS, "", "已经收藏了");
        }
        return new JsonResult(ResultCode.FAIL, "", "未收藏了");

//        return new JsonResult(ResultCode.SUCCESS);
    }

    //返回图片信息
    public JsonResult getPictureInfor(String pictureId, String username) {
        String accountId = personalHubMapper.getAccountId(username);
        List<PictureInfor> temp = personalHubMapper.getPictureInfor(pictureId);
        PictureInfor pictureInfor = new PictureInfor();
        if (temp.size() == 0) {
            pictureInfor.setPicturepath(personalHubMapper.getPicturepath(pictureId));
            return new JsonResult(ResultCode.SUCCESS,pictureInfor);
        }
        List<String> tagName = new ArrayList();

        //图片路径
        pictureInfor.setPicturepath(temp.get(0).getPicturepath());

        List<String> favoritePicture = personalHubMapper.getFavoritePicture(accountId);
        String isLikePicture = personalHubMapper.isLike(accountId, pictureId);
        //是否收藏过
        if (favoritePicture.contains(pictureId)) {
            pictureInfor.setFavorite(true);
        } else {
            pictureInfor.setFavorite(false);
        }
        //是否喜欢过
        if (isLikePicture == null) {
            pictureInfor.setLike(false);
        } else {
            pictureInfor.setLike(true);
        }
        //图片的属性
        temp.stream().forEach(x -> {
            tagName.add(x.getTagName());
        });
        pictureInfor.setTagList(tagName);
        //被喜欢的个数
        pictureInfor.setFavoriteNum(personalHubMapper.userLikeNum(pictureId));
        pictureInfor.setFavoriteNum(personalHubMapper.userFavoriteNum(pictureId));
        PersonalInformation per = personalHubMapper.getFaBuInfor(pictureId);
        if (per == null) {
            return new JsonResult(ResultCode.SUCCESS, pictureInfor);
        }
        pictureInfor.init(per, personalHubMapper.getFaBunum(per.getAccount_id()));
        return new JsonResult(ResultCode.SUCCESS, pictureInfor);
    }
    
     //返回图片信息(未登录)
    public JsonResult getNoLoginPictureInfor(String pictureId) {
        List<PictureInfor> temp = personalHubMapper.getPictureInfor(pictureId);
        PictureInfor pictureInfor = new PictureInfor();
        if (temp.size() == 0) {
            pictureInfor.setPicturepath(personalHubMapper.getPicturepath(pictureId));
            return new JsonResult(ResultCode.SUCCESS,pictureInfor);
        }
        List<String> tagName = new ArrayList();

        //图片路径
        pictureInfor.setPicturepath(temp.get(0).getPicturepath());
        
        //图片的属性
        temp.stream().forEach(x -> {
            tagName.add(x.getTagName());
        });
        pictureInfor.setTagList(tagName);
        //被喜欢的个数
        pictureInfor.setFavoriteNum(personalHubMapper.userLikeNum(pictureId));
        pictureInfor.setFavoriteNum(personalHubMapper.userFavoriteNum(pictureId));
        PersonalInformation per = personalHubMapper.getFaBuInfor(pictureId);
        if (per == null) {
            return new JsonResult(ResultCode.SUCCESS, pictureInfor);
        }
        pictureInfor.init(per, personalHubMapper.getFaBunum(per.getAccount_id()));
        return new JsonResult(ResultCode.SUCCESS, pictureInfor);
    }
}
